/**
 * @class Ext.Ajax
 * @extends Ext.data.Connection
 * <p>重写官方Ext.Ajax类, 增加了{@link #async async}属性, 支持同步AJAX交互.</p>
 * @singleton
 */
Ext.Ajax = new Ext.data.Connection({
    defaultHeaders: {
        'Powered-By': 'ExtPlus'
    },

    /**
     * True标识Ajax Request为异步AJAX交互, False则为同步. (defaults to true)
     * @property async
     * @type Boolean
     */
    async: true,

    autoAbort: false,

    serializeForm: function(form) {
        return Ext.lib.Ajax.serializeForm(form);
    },

    request: function(o) {
        var me = this;
        if (me.fireEvent('beforerequest', me, o)) {
            if (o.el) {
                if (!Ext.isEmpty(o.indicatorText)) {
                    me.indicatorText = '<div class="loading-indicator">' + o.indicatorText + "</div>";
                }
                if (me.indicatorText) {
                    Ext.getDom(o.el).innerHTML = me.indicatorText;
                }
                o.success = (Ext.isFunction(o.success) ? o.success : function() {
                }).createInterceptor(function(response) {
                    Ext.getDom(o.el).innerHTML = response.responseText;
                });
            }

            var p = o.params, url = o.url || me.url, method, async, cb = {
                success: me.handleResponse,
                failure: me.handleFailure,
                scope: me,
                argument: {
                    options: o
                },
                timeout: o.timeout || me.timeout
            }, form, serForm;

            if (Ext.isFunction(p)) {
                p = p.call(o.scope || window, o);
            }

            p = Ext.urlEncode(me.extraParams, Ext.isObject(p) ? Ext.urlEncode(p) : p);

            if (Ext.isFunction(url)) {
                url = url.call(o.scope || window, o);
            }

            if ((form = Ext.getDom(o.form))) {
                url = url || form.action;
                if (o.isUpload || /multipart\/form-data/i.test(form.getAttribute("enctype"))) {
                    return me.doFormUpload.call(me, o, p, url);
                }
                serForm = Ext.lib.Ajax.serializeForm(form);
                p = p ? (p + '&' + serForm) : serForm;
            }

            method = o.method || me.method || ((p || o.xmlData || o.jsonData) ? 'POST' : 'GET');

            async = Ext.isDefined(o.async) ? o.async : me.async;

            if (method === 'GET' && (me.disableCaching && o.disableCaching !== false) || o.disableCaching === true) {
                var dcp = o.disableCachingParam || me.disableCachingParam;
                url = Ext.urlAppend(url, dcp + '=' + (new Date().getTime()));
            }

            o.headers = Ext.apply(o.headers || {}, me.defaultHeaders || {});

            if (o.autoAbort === true || me.autoAbort) {
                me.abort();
            }

            if ((method == 'GET' || o.xmlData || o.jsonData) && p) {
                url = Ext.urlAppend(url, p);
                p = '';
            }

            if (async) { // 异步Ajax请求
                if (o.task) {
                    // 增加一个Ajax请求到AjaxTask中
                    var task;
                    if (task = Ext.AjaxTask.get(o.task)) {
                        var requestConfig = {
                            uri: o.url,
                            callback: cb,
                            params: p,
                            options: o
                        };
                        Ext.AjaxTask.addRequest(task, requestConfig);
                        return null;
                    }
                }
                return (me.transId = Ext.lib.Ajax.request(method, url, cb, p, o));
            } else {
                return (me.transId = Ext.lib.Ajax.syncRequest(method, url, cb, p, o));
            }
        } else {
            return o.callback ? o.callback.apply(o.scope, [o, undefined, undefined]) : null;
        }
    }
});
